package util

import (
	"ecovacs.gitlab.com/NLP/nlp-go-core.git/log"
	"os"
	"syscall"
	"time"
)

func Dump() {
	fileName := time.Now().Format("2006-01-02")
	logFilename := "/home/logs/" + fileName + ".log"
	logFile, err := os.OpenFile(logFilename, os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0644)
	if nil != err {
		log.Error("os.OpenFile err: %v", err)
		return
	}
	redirectStderr(logFile)
	os.Stderr.Write([]byte(time.Now().Format("2006-01-02 15:04:05") + "\n"))
}

// redirectStderr to the file passed in
func redirectStderr(f *os.File) {
	os.Stderr = f
	err := syscall.Dup2(int(f.Fd()), 2)
	if err != nil {
		log.Error("Failed to redirect stderr to file: %v", err)
	}
}
